// ==UserScript==
// @name           URL Suffix
// @namespace      http://www.quchao.com/
// @description    Adding suffix to url by pressing different keys.
// @include        main
// @compatibility  Firefox 2.0.* 3.6a1pre
// @author         Qu Chao (Chappell.Wat) <Chappell.Wat@Gmail.com>
// @version        1.0
// @Note
// ==/UserScript==
// Released under the GPL license
//  http://www.gnu.org/copyleft/gpl.html
// Appreciate to
//  Gomyoung Kim (http://heygom.com/extensions/)
// ver 1.0 @ 2008-7-17
//  Initialize release

(function() {
	var UrlSuffix = function () {
		const canonizeUrl = function (aTriggeringEvent) {
				var url = this.value;
				if (!url) {
					return ["", null];
				}
				if (!/\x20/.test(url) && !/^(www|http|chrome|ftp|https)|\/\s*$/i.test(url) && aTriggeringEvent instanceof KeyEvent) {
					let accel = aTriggeringEvent.ctrlKey;
					let shift = aTriggeringEvent.shiftKey;
					let alt = aTriggeringEvent.altKey;
					let suffix = "";
					switch (true) {
					  case accel && shift && alt:
						suffix = ".org.cn/";
						break;
					  case accel && shift:
						suffix = ".org/";
						break;
					  case accel && alt:
						suffix = ".com.cn/";
						break;
					  case shift && alt:
						suffix = ".net.cn/";
						break;
					  case shift:
						suffix = ".net/";
						break;
					  case alt:
						suffix = ".cn/";
						break;
					  case accel:
						try {
							suffix = gPrefService.getCharPref("browser.fixup.alternate.suffix");
							if (suffix.charAt(suffix.length - 1) != "/") {
								suffix += "/";
							}
						} catch (e) {
							suffix = ".com/";
						}
						break;
					  default:;
					}
					if (suffix) {
						url = url.trim();
						let firstSlash = url.indexOf("/");
						let existingSuffix = url.indexOf(suffix.substring(0, suffix.length - 1));
						if (firstSlash >= 0) {
							if (existingSuffix == -1 || existingSuffix > firstSlash) {
								url = url.substring(0, firstSlash) + suffix + url.substring(firstSlash + 1);
							}
						} else {
							url = url + (existingSuffix == -1 ? suffix : "/");
						}
						url = "http://www." + url;
					}
				}
				var postData = {};
				url = getShortcutOrURI(url, postData);
				return [url, postData.value];
			};
		return {
			init: function () {
				gURLBar._canonizeURL = canonizeUrl;
			}
		};
	}();
	UrlSuffix.init();
})();